import KeyAble from "../interface/KeyAble";

export default class TSObjectPool<T extends KeyAble> {

    private pool:any [] = []


    private className:string;

    constructor(className:string,type: { new(): T ;},count:number = 0){
        this.className = className;
        for (let index = 0; index < count; index++) {
            this.pool.push(new type());
        }
    }

    getClassName(){
        return this.className;
    }

    get<T extends KeyAble>(type: { new(): T ;} ): T {
        let go = this.pool.length > 0 ? this.pool.shift() : null;
        if(!go){
            go = new type();
        }
        return go;
    }

    put(instance:T){
        this.pool.push(instance);

    }


    clear(){
        this.pool = [];
    }
 
}
